package com.garen.fire.biz.impl;

import com.garen.fire.dao.IFireCarDao;
import com.garen.fire.dao.IFireCarGpsDao;
import com.garen.fire.dao.IFireCaseInfoDao;
import com.garen.fire.dao.IFireWellDao;
import com.garen.fire.entity.FireCar;
import com.garen.fire.entity.FireCaseInfo;
import com.garen.sys.biz.IFireOrgBiz;
import com.garen.sys.dao.IFireOrgDao;
import com.garen.sys.dao.IFireUserGpsDao;
import com.garen.sys.entity.FireOrg;
import com.garen.utils.DateUtils;
import com.garen.utils.FileUtils;
import com.garen.fire.biz.IFireCaseInfoBiz;
import com.garen.fire.biz.IJPushBiz;

import org.springframework.stereotype.Service;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.alibaba.fastjson.JSONObject;
import com.garen.common.*;


@Service
public class FireCaseInfoBizImpl implements IFireCaseInfoBiz {

	
	private static final String CAR_STATUS_FIGHT = "10"; //作战状态
	private static final String CAR_STATUS_STANDBY = "1";	//待命状态
	private static final String CAR_STATUS_REPAIR = "2";	//维修状态
	
	@Autowired
	private IFireCaseInfoDao fireCaseInfoDao;

	private static String SOUND_URL = "http://192.168.1.4:12000/?filename=";
	protected static Log log = LogFactory.getLog(FireCaseInfoBizImpl.class);

	@Autowired
	private IFireWellDao fireWellDao;
	
	@Autowired
	private IFireOrgBiz iFireOrgBiz;
	
	
	private List<FireOrg>  orgList;
	@Autowired
	private IFireCarGpsDao fireCarGpsDao;
	@Autowired
	private IFireUserGpsDao fireUserGpsDao;
	@Autowired
	private IFireOrgDao fireOrgDao;
	@Autowired
	private IFireCarDao fireCarDao;
	
	@Autowired
	private IJPushBiz jPushBiz;

	//复制新案情
	public void copyCase(MsgBean mbean, FireCaseInfo fci) {
		fci = fireCaseInfoDao.get(fci);
		if(null == fci) return ;
		//清空主键
		fci.setCaseId(null);
		//修正案件编号
		String ajbh = fci.getAjbh();
		fci.setRemark("复制于" + ajbh);
		//String order = ajbh.substring(ajbh.length() - 5, ajbh.length());
		ajbh = DateUtils.date2str(new Date(),"yyyyMMddHHmmss");
		//ajbh += "A";
		fci.setAjbh(ajbh);
		//修正地址及备注
		String zhdz = fci.getZhdz();
		fci.setZhdz(zhdz + "----测试");
		//修正报警时间
		fci.setBjsj(new Date());
		//重置状态
		fci.setStatus(FireCaseInfo.STATUS_INIT);
		fci.setKssj(null);
		fci.setJssj(null);
		fireCaseInfoDao.addAndId(fci);
		jPushBiz.pushMessage(IJPushBiz.JPUSH_TYPE_NEW_FIGHT, fci);
	}
	/*
	 * 获取案情关联的GPS信息
	 * */
	public JSONObject getFireCaseGps(String ajbh) {
		JSONObject gpsJson = new JSONObject();
		gpsJson.put("carGps", fireCarGpsDao.getFireCarGpsListByAjbh(ajbh));
		gpsJson.put("userGps", fireUserGpsDao.getFireUserGpsListByAjbh(ajbh));
		gpsJson.put("orgGps", fireOrgDao.getFireOrgByAjbh(ajbh));
		return gpsJson;
	}
	
	//抓取案情派遣车辆信息
	public Set<String> fetchCaseCars(String ajbh){
		List<Map<String,Object>> caseList = fireWellDao.getCaseCars(ajbh);
		StringBuilder sb = new StringBuilder();
		sb.append("\n");
		Set<String> carSet = new HashSet<>();
		for(Map<String,Object> map : caseList){
			String carstr = map.get("ACTINF").toString();
			String [] carList  = carstr.split("##");
			String orgId = map.get("CMDORGID").toString();
			sb.append("消防队ID:" + orgId  + "\n");
			for(String car : carList){
				if(StringUtils.isEmpty(car)) continue;
				//FireCaseCar fireCaseCar = new FireCaseCar();
				//fireCaseCar.setAjbh(ajbh);
				//fireCaseCar.setOrgId(orgId);
				String[] hash = car.split("=");
				if(hash.length != 2) continue;
				//log.debug(hash[0] + "="+ hash[1]);
				if( hash[0].matches("MaterID\\d+")){
					//fireCaseCar.setFireCar(hash[1]);
					//fireCaseCarDao.add(fireCaseCar);//存库
					//caseCarList.add(fireCaseCar);
					carSet.add(hash[1]);//存入车主键
				}
				sb.append("\t" + hash[0] + "="+ hash[1]+ "\n");
			}
		}
		//log.debug(sb);
		return carSet;
	}
		
	
	/*
	*保存
	*/
	@Override
	public int saveFireCaseInfo(MsgBean mbean,FireCaseInfo fireCaseInfo){
		String status = fireCaseInfo.getStatus();
		String ajbh = fireCaseInfo.getAjbh();
		if(FireCaseInfo.STATUS_GO.equals(status)){
			fireCaseInfo.setKssj(new Date());
			jPushBiz.pushMessage(IJPushBiz.JPUSH_TYPE_SET_OUT, ajbh);
		}
		else if(FireCaseInfo.STATUS_OVER.equals(status))
		{
			fireCaseInfo.setJssj(new Date());
			jPushBiz.pushMessage(IJPushBiz.JPUSH_TYPE_COMPLETE, ajbh);

		}
		else if(FireCaseInfo.STATUS_REACH.equals(status))
		{
			jPushBiz.pushMessage(IJPushBiz.JPUSH_TYPE_ARRIVE, ajbh);
		}
		return fireCaseInfoDao.updateByField(fireCaseInfo,"ajbh");
	}
	
	/*
	*保存
	*/
	@Override
	public int updateFireCaseInfo(MsgBean mbean,FireCaseInfo fireCaseInfo){
		return fireCaseInfoDao.update(fireCaseInfo);
	}

	//查询案情录音是否存在
	public boolean queryCaseSound(String ajbh) {
		Map<String,Object> sound = fireWellDao.getCaseSound(ajbh);
		return (sound == null)?false:true;
	};
	
	@Override
	public byte[] caseSound(String ajbh) {
		Map<String,Object> sound = fireWellDao.getCaseSound(ajbh);
		if(sound == null) return null;
		String url = SOUND_URL + sound.get("FILEPATH") + sound.get("FILENAME");
		byte [] body = FileUtils.getHtml4Byte(url, null);
		return body;
	}
	
	
	/*
	*分页查询
	*/
	@Override
	public List<FireCaseInfo> getFireCaseInfoList(PageBean<FireCaseInfo> pb,FireCaseInfo fireCaseInfo){
		return fireCaseInfoDao.getFireCaseInfoList(pb, fireCaseInfo);
	}

	/*
	*
	*/
	@Override
	public List<FireCaseInfo> getFireCaseInfoList(FireCaseInfo fireCaseInfo,Integer pid){
		List<Integer> orgIds = iFireOrgBiz.getOrgChildrenID(pid);
		return fireCaseInfoDao.getFireCaseInfoList(fireCaseInfo,orgIds);
	}
	/*
	*
	*/
	@Override
	public List<FireCaseInfo> getFireCaseInfoListPad(FireCaseInfo fireCaseInfo,Integer pid){
		List<Integer> orgIds = iFireOrgBiz.getOrgChildrenID(pid);
		return fireCaseInfoDao.getFireCaseInfoListPad(fireCaseInfo,orgIds);
	}

	@Override
	public List<FireCaseInfo> getFireCaseInfoList(PageBean<FireCaseInfo> pb, Integer pid, Date kssj, Date jssj) {
		List<Integer> orgIdList = iFireOrgBiz.getOrgChildrenID(pid);
		return fireCaseInfoDao.getFireCaseInfoList(pb,orgIdList,kssj,jssj);
	}

	@Override
	public List<FireCaseInfo> getCaseInfoByCar(String carId) {
		return fireCaseInfoDao.getCaseInfoByCar(carId);
	}

	@Override
	public List<Map<String, Object>> statuscount(FireCaseInfo fireCaseInfo,Integer orgId) {
		
		return fireCaseInfoDao.getstatuscount(fireCaseInfo,orgId);
	}
	@Override
	public List<Map<String, Object>> getCarCountByOrgId(Integer orgId) {
		// TODO Auto-generated method stub
		List<FireCar> fireCarList = this.fireCarDao.getAllFireCarListByOrgId(orgId);
		int sum = 0;
		int standbyNum = 0;
		int repairNum = 0;
		int fightNum = 0;
		
		if(fireCarList!=null&&fireCarList.size()>0)
		{
			sum = fireCarList.size();
			for (FireCar fireCar : fireCarList) {
				if(CAR_STATUS_FIGHT.equals(fireCar.getCarStatus()))
				{
					fightNum++;
				}
				else if(CAR_STATUS_REPAIR.equals(fireCar.getCarStatus()))
				{
					repairNum++;
				}
				else
				{
					standbyNum++;
				}
			}
		}
		
		
		
		Map<String,Object> carCountMap = new HashMap<String, Object>();
		carCountMap.put("sum", sum);
		carCountMap.put("standby", standbyNum);
		carCountMap.put("repair", repairNum);
		carCountMap.put("fight", fightNum);
		List<Map<String, Object>> resultList = new ArrayList<Map<String,Object>>();
		resultList.add(carCountMap);
		
		return resultList;
	}
	@Override
	public List<Map<String, Object>> getCarCountByOrgId(List<Integer> orgIdList) {
		// TODO Auto-generated method stub
		List<FireCar> fireCarList = this.fireCarDao.getAllFireCarListByOrgId(orgIdList);
		int sum = 0;
		int standbyNum = 0;
		int repairNum = 0;
		int fightNum = 0;
		
		if(fireCarList!=null&&fireCarList.size()>0)
		{
			sum = fireCarList.size();
			for (FireCar fireCar : fireCarList) {
				if(CAR_STATUS_FIGHT.equals(fireCar.getCarStatus()))
				{
					fightNum++;
				}
				else if(CAR_STATUS_REPAIR.equals(fireCar.getCarStatus()))
				{
					repairNum++;
				}
				else
				{
					standbyNum++;
				}
			}
		}
		
		
		
		Map<String,Object> carCountMap = new HashMap<String, Object>();
		carCountMap.put("sum", sum);
		carCountMap.put("standby", standbyNum);
		carCountMap.put("repair", repairNum);
		carCountMap.put("fight", fightNum);
		List<Map<String, Object>> resultList = new ArrayList<Map<String,Object>>();
		resultList.add(carCountMap);
		
		return resultList;
	}
	
	

}
